package org.apache.jsp.ForumHtml;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import java.rmi.Naming;
import clientData.MemberDataInt;
import java.util.LinkedList;
import server.ForumHandlerInt;
import java.rmi.Naming;
import server.ForumHandlerInt;

public final class addFriend_jsp extends org.apache.jasper.runtime.HttpJspBase
    implements org.apache.jasper.runtime.JspSourceDependent {

  private static final javax.servlet.jsp.JspFactory _jspxFactory =
          javax.servlet.jsp.JspFactory.getDefaultFactory();

  private static java.util.List<java.lang.String> _jspx_dependants;

  static {
    _jspx_dependants = new java.util.ArrayList<java.lang.String>(1);
    _jspx_dependants.add("/ForumHtml/serverConnection.jsp");
  }

  private javax.el.ExpressionFactory _el_expressionfactory;
  private org.apache.tomcat.InstanceManager _jsp_instancemanager;

  public java.util.List<java.lang.String> getDependants() {
    return _jspx_dependants;
  }

  public void _jspInit() {
    _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
    _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());
  }

  public void _jspDestroy() {
  }

  public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
        throws java.io.IOException, javax.servlet.ServletException {

    final javax.servlet.jsp.PageContext pageContext;
    javax.servlet.http.HttpSession session = null;
    final javax.servlet.ServletContext application;
    final javax.servlet.ServletConfig config;
    javax.servlet.jsp.JspWriter out = null;
    final java.lang.Object page = this;
    javax.servlet.jsp.JspWriter _jspx_out = null;
    javax.servlet.jsp.PageContext _jspx_page_context = null;


    try {
      response.setContentType("text/html; charset=windows-1255");
      pageContext = _jspxFactory.getPageContext(this, request, response,
      			null, true, 8192, true);
      _jspx_page_context = pageContext;
      application = pageContext.getServletContext();
      config = pageContext.getServletConfig();
      session = pageContext.getSession();
      out = pageContext.getOut();
      _jspx_out = out;

      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\t");

		//System.out.println("adding friend");
		String friendUserName = request.getParameter("friend");
		//System.out.println("friend:"+friendUserName);
		//String userName = "nir123";
		String userName=(String) session.getAttribute("userName");
		if(userName==null) userName="";

		//String password = "123";
		String password=(String) session.getAttribute("password");
		if(password==null) password="";
		
		

	
      out.write("\r\n");
      out.write("\r\n");
      out.write("\t");
      out.write("\r\n");
      out.write("\r\n");

	
   String serverDomain = "localhost";
   ForumHandlerInt forum = null;
  
     try
     {
   	 forum =  (ForumHandlerInt)Naming.lookup("rmi://"+serverDomain+"/ForumService");
	   }
   catch (Exception e){
	   
	   
	   response.sendError(response.SC_INTERNAL_SERVER_ERROR,"No connection to the forum server.");
	   response.flushBuffer();
		return;
   }

      out.write("\r\n");
      out.write("\r\n");
      out.write("\t");

		try{
			forum.addFriend(userName, password, friendUserName);
		}
		catch(Exception e){
			
      out.print(e.getMessage());
      out.write(' ');

		}
	
	
		LinkedList<MemberDataInt> friends = forum.getUserFriends(userName, password);
	
      out.write("\r\n");
      out.write("\t<select id=\"friendsList\" name=\"friendsList\" size=\"10\" >\r\n");
      out.write("\t\t");

			for(MemberDataInt friend : friends){	
				
      out.write("<option value=\"");
      out.print( friend.getUserName());
      out.write('"');
      out.write('>');
      out.print( friend.getUserName());
      out.write("</option>");

			}
		
      out.write("\r\n");
      out.write("\t</select>\r\n");
      out.write("\r\n");
      out.write("\r\n");
    } catch (java.lang.Throwable t) {
      if (!(t instanceof javax.servlet.jsp.SkipPageException)){
        out = _jspx_out;
        if (out != null && out.getBufferSize() != 0)
          try { out.clearBuffer(); } catch (java.io.IOException e) {}
        if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
      }
    } finally {
      _jspxFactory.releasePageContext(_jspx_page_context);
    }
  }
}
